package com.koron.ticket.work.logicticket.ycpowerlinefirst;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.koron.bean.base.Response;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.FastJsonUtils;
import com.koron.ticket.utils.StringUtils;
import com.koron.ticket.work.common.bean.SecurityArrangementBean;
import com.koron.ticket.work.common.bean.TicketBaseMsg;
import com.koron.ticket.work.common.service.FifteenTablesService;
import com.koron.ticket.work.common.service.YcCommonService;
import com.koron.ticket.work.enums.PrintTemplate;
import com.koron.ticket.work.enums.TicketNodeStatus;
import com.koron.ticket.work.enums.TicketStatus;
import com.koron.ticket.work.logicticket.ycpowerlinefirst.bean.SignDto;
import com.koron.ticket.work.logicticket.ycpowerlinefirst.bean.YcLineFirstDto;
import com.koron.ticket.work.logicticket.ycpowerlinefirst.bean.YcLineFirstPrint;
import com.koron.ticket.work.workPermit.bean.WorkPermit;
import com.koron.ticket.work.workPermit.service.WorkPermitService;
import com.koron.ticket.work.workend.bean.WorkEnd;
import com.koron.ticket.work.workend.service.WorkEndService;
import com.koron.ticket.work.workflow.bean.WorkFlow;
import com.koron.ticket.work.workflow.service.WorkFlowService;
import com.koron.ticket.work.workmember.bean.WorkMember;
import com.koron.ticket.work.workteam.bean.WorkTeam;
import com.koron.ticket.work.workticket.bean.WorkTicket;
import com.koron.ticket.work.workticket.service.WorkTicketService;
import com.koron.ticket.work.workyccontent.bean.YcContentBean;
import com.koron.ticket.work.workyccontent.service.YcContentService;
import com.koron.ticket.work.workycinfo.bean.TicketYcInfoBean;
import com.koron.ticket.work.workycinfo.service.WorkYcInfoService;
import com.koron.ticket.workchange.ticketchange.bean.TicketChange;
import com.koron.ticket.workchange.ticketchange.service.TicketChangeService;
import com.koron.ticket.workchange.ticketchangedelay.bean.TicketChangeDelay;
import com.koron.ticket.workchange.ticketchangedirector.bean.TicketChangeDirector;
import com.koron.ticket.workchange.ticketchangemember.bean.TicketChangeMember;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class YcLineFirstService {

    @Autowired
    private FifteenTablesService fifteenTablesService;

    @Autowired
    private WorkFlowService workFlowService;

    @Autowired
    private WorkEndService workEndService;

    @Autowired
    private WorkTicketService workTicketService;

    @Autowired
    private WorkPermitService workPermitService;

    @Autowired
    private WorkYcInfoService workycinfoservice;

    @Autowired
    private YcCommonService yccommonservice;

    @Autowired
    private YcContentService yccontentservice;

    @Autowired
    private WorkEndService workendservice;

    @Autowired
    private TicketChangeService ticketChangeService;

    @TaskAnnotation("writeSave")
    public Response writeSave(SessionFactory factory, YcLineFirstDto dto) {
//        workTicketService.checkTimeOut(factory, dto.getId(), dto.getUpdateTime());//检查信息是否过期
        String id = CodeTools.getCode32();

        // 工作票主表
        WorkTicket workticket = Convert.convert(WorkTicket.class, dto);
        // 线路二种作业票
        TicketYcInfoBean ticketycinfobean = Convert.convert(TicketYcInfoBean.class, dto);
        // 工作票基础信息
        TicketBaseMsg ticketbasemsg = Convert.convert(TicketBaseMsg.class, dto);

        ticketycinfobean.setTicketType(workticket.getType());
        if (StrUtil.isNotBlank(workticket.getId())) {
            workTicketService.update(factory, workticket);
            ticketycinfobean.setWorkTicketId(workticket.getId());
        } else {
            workticket.setId(id);
            ticketycinfobean.setWorkTicketId(id);
            workTicketService.insert(factory, workticket);
        }
        workycinfoservice.save(factory, ticketycinfobean);
        yccontentservice.save(factory, dto.getContentlist(), workticket.getId(), PrintTemplate.YcPowerLineSecond.getType());

        // 更新工作票基础信息
        yccommonservice.updateTicketMsg(factory, workticket.getId(), ticketbasemsg);

        return Response.success(workticket.getId());
    }

    @TaskAnnotation("signSave")
    public Response signSave(SessionFactory factory, SignDto dto) {
        workTicketService.checkTimeOut(factory, dto.getWorkTicketId(), dto.getUpdateTime());//检查信息是否过期

        // 删除签名表签发节点的签名
        workFlowService.deleteByWorkTicketId(factory, dto.getWorkTicketId(), TicketStatus.SIGN.getStatus());

        if (CollUtil.isEmpty(dto.getWorkFlowList())) {
            return Response.success("提交/保存成功");
        }
        dto.getWorkFlowList().stream().forEach(w -> w.setWorkTicketId(dto.getWorkTicketId()));
        workFlowService.batchInsert(factory, dto.getWorkFlowList());

        return Response.success("提交/保存成功");
    }

//    @TaskAnnotation("countersignSave")
//    public Response countersignSave(SessionFactory factory, WorkFlow workFlow) {
//        yccommonservice.countersignSave(factory, workFlow);
//        return Response.success("提交/保存成功");
//    }

//    @TaskAnnotation("acceptSave")
//    public Response acceptSave(SessionFactory factory, AcceptDto dto) {
//        workTicketService.checkTimeOut(factory, dto.getWorkTicketId(), dto.getUpdateTime());//检查信息是否过期
//        String workTicketId = dto.getWorkTicketId();
//        List<WorkFlow> workFlowList = dto.getWorkFlowList();
//
//        TicketYcInfoBean ticketycinfobean = new TicketYcInfoBean();
//        ticketycinfobean.setWorkTicketId(dto.getWorkTicketId());
//        ticketycinfobean.setWorkStartTime(dto.getWorkStartTime());
//        ticketycinfobean.setWorkEndTime(dto.getWorkEndTime());
//
//        // 更新工作开始完成日期
//        workycinfoservice.updateSelective(factory, ticketycinfobean);
//
//        // 删除签名表许可节点的签名
//        workFlowService.deleteByWorkTicketId(factory, dto.getWorkTicketId(), TicketStatus.ACCEPT.getStatus());
//        workFlowService.batchInsert(factory, workFlowList);
//        return Response.success("提交/保存成功");
//    }

    @TaskAnnotation("permitSave")
    public Response permitSave(SessionFactory factory, WorkPermit dto) {
        workTicketService.checkTimeOut(factory, dto.getWorkTicketId(), dto.getUpdateTime());//检查信息是否过期
        List<WorkFlow> workFlowList = dto.getWorkFlowList();

        // 删除签名表许可节点的签名
        workFlowService.deleteByWorkTicketId(factory, dto.getWorkTicketId(), TicketStatus.ACCEPT.getStatus());
        workFlowService.batchInsert(factory, workFlowList);
        return Response.success("待许可-提交/保存成功");
    }

    @TaskAnnotation("ticketEndSave")
    public Response ticketEndSave(SessionFactory factory, WorkEnd workend) {
        String workTicketId = workend.getWorkTicketId();
        TicketYcInfoBean ycinfo = new TicketYcInfoBean();
        ycinfo.setWorkTicketId(workTicketId);
        ycinfo.setRemark(workend.getRemark());

        workEndService.deleteByWorkTicketId(factory, workTicketId);
        workend.setStatus(TicketStatus.TICKET_END.getStatus());
        workEndService.insert(factory, workend);

        workFlowService.deleteByWorkTicketId(factory, workTicketId, TicketStatus.TICKET_END.getStatus());
        if (CollUtil.isNotEmpty(workend.getWorkFlowList())) {
            workend.getWorkFlowList().stream().forEach(w -> w.setWorkTicketId(workTicketId));
            workFlowService.batchInsert(factory, workend.getWorkFlowList());
        }

        workycinfoservice.updateSelective(factory, ycinfo);

        return Response.success("待许可-提交/保存成功");
    }

//    @TaskAnnotation("ticketEndSave")
//    public Response ticketEndSubmitOrSave(SessionFactory factory, WorkEnd workEnd) {
//        List<WorkFlow> workFlowList = workEnd.getWorkFlowList();
//        workFlowService.batchInsert(factory, workFlowList);
//        return Response.success("工作票待终结-提交/保存成功");
//    }

    @TaskAnnotation("workflowUpdate")
    public Response workflowUpdate(SessionFactory factory, RedirectQuery redirectQuery) throws ParseException {
        Integer operation = redirectQuery.getOperation();
        String workTicketId = redirectQuery.getDataId();
        WorkTicket workTicket = workTicketService.queryById(factory, workTicketId);
        // 提交前状态
        Integer status = workTicket.getStatus();
        String type = workTicket.getType();

        // 1. 如果被驳回，返回对应状态，仅删除签名的数据，其他的数据不做处理
        if (operation == 1) {
            String rejectNodeCode = redirectQuery.getRejectNodeCode();
            rejectNodeCode = rejectNodeCode.replace("call-", "");
            if (rejectNodeCode.equals(TicketNodeStatus.WRITE.getNodeName())) {
                Integer rejectStatus = TicketStatus.WRITE.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.SIGN.getNodeName())) {
                Integer rejectStatus = TicketStatus.SIGN.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.COUNTERSIGN.getNodeName())) {
                Integer rejectStatus = TicketStatus.COUNTERSIGN.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.ACCEPT.getNodeName())) {
                Integer rejectStatus = TicketStatus.ACCEPT.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
                workTicket.setStatus(rejectStatus);
            } else if (StrUtil.contains(rejectNodeCode, TicketNodeStatus.PERMIT.getNodeName())) {
                // 高处作业有两个许可，平台返回的code 包含PERMIT都认为是许可节点
                Integer rejectStatus = TicketStatus.PERMIT.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                // 20211111: 如何是待许可状态，讲预览标志设置成未预览
                workTicket.setPreviewFlag(1);
                workTicket.setStatus(rejectStatus);
            } else if (rejectNodeCode.equals(TicketNodeStatus.WORK_END.getNodeName())) {
                Integer rejectStatus = TicketStatus.WORK_END.getStatus();
                fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
                workTicket.setStatus(rejectStatus);
            }
            workTicketService.update(factory, workTicket);
        } // 2. 如果通过，工作票进入下一个状态，并根据状态的不同进行不同的操作
        else if (operation == 32) {
            // 下一个节点编码
            String curNodeCode = redirectQuery.getCurNodeCode();
            curNodeCode = curNodeCode.replace("call-", "");
            String formVariables = redirectQuery.getFormVariables();
            if (status.equals(TicketStatus.WRITE.getStatus())) {
                workTicket.setStatus(2);
            }
            // 2.1 如果是签发状态，保存签名信息
            if (status.equals(TicketStatus.SIGN.getStatus())) {
                // 保存许可信息
                SignDto dto = JSONObject.parseObject(formVariables, SignDto.class);
                workFlowService.deleteByWorkTicketId(factory, workTicketId, TicketStatus.SIGN.getStatus());
                workFlowService.batchInsert(factory, dto.getWorkFlowList());
                // 生成工作票号
                if (StrUtil.isBlank(workTicket.getCode())) {
                    String code = fifteenTablesService.getLastCodeByUnitAndTicketTypeNew(factory, workTicket.getUnit(), type);
                    if (StringUtils.isEmpty(code)) {
                        return Response.fail("工作票票号生成有问题，请联系运维人员");
                    }
                    workTicket.setCode(code);
                }
                workTicket.setStatus(5);//放弃自增控制，改为手动控制
            }
//            else if (status.equals(TicketStatus.ACCEPT.getStatus())) {
//                // 保存签名信息
//                fifteenTablesService.saveWorkFLow(factory, formVariables);
//                AcceptDto acceptdto = JSONObject.parseObject(formVariables, AcceptDto.class);
//                this.acceptSave(factory, acceptdto);
//                workTicket.setStatus(7);//放弃自增控制，改为手动控制
//            }
            // 2.4 如果是待许可状态，保存签名信息，以及待许可信息
            else if (status.equals(TicketStatus.PERMIT.getStatus())) {
                // 保存许可信息
                WorkPermit workPermit = JSONObject.parseObject(formVariables, WorkPermit.class);
                // 保存签名信息
                List<WorkFlow> workFlowList = workPermit.getWorkFlowList();
                if (CollUtil.isNotEmpty(workFlowList)) {
                    for (WorkFlow workFLow : workFlowList) {
                        workFlowService.save(factory, workFLow);
                    }
                }
                workTicket.setStatus(7);//放弃自增控制，改为手动控制
            }
//            else if (status.equals(TicketStatus.WORK_END.getStatus())) {
//                WorkEnd workend = FastJsonUtils.getJsonToBean(formVariables, WorkEnd.class);
//                List<WorkFlow> workFlowList = workend.getWorkFlowList();
//                for (WorkFlow workFlow : workFlowList) {
//                    workFlowService.save(factory, workFlow);
//                }
//                workTicket.setStatus(7);//放弃自增控制，改为手动控制
//            }
            workTicketService.update(factory, workTicket);
        } else if (operation == 256) {
            Integer rejectStatus = TicketStatus.WRITE.getStatus();
            fifteenTablesService.clenWorkFlowByStatus(factory, workTicketId, rejectStatus);
            workPermitService.deleteByWorkTicketId(factory, workTicketId);//删除许可信息
            workTicket.setStatus(rejectStatus);
            workTicketService.update(factory, workTicket);
        } else {
            return Response.fail("操作类型为：" + operation + ",请联系运维人员");
        }

        return Response.success("提交成功");
    }

    @TaskAnnotation("workflowUpdateEnd")
    public Response workflowUpdateEnd(SessionFactory factory, RedirectQuery redirectQuery) throws ParseException {
        String formVariables = redirectQuery.getFormVariables();
        WorkEnd workend = FastJsonUtils.getJsonToBean(formVariables, WorkEnd.class);

        workEndService.deleteByWorkTicketId(factory, workend.getWorkTicketId());
        workend.setStatus(TicketStatus.TICKET_END.getStatus());
        workEndService.insert(factory, workend);

        List<WorkFlow> workFlowList = workend.getWorkFlowList();
        for (WorkFlow workFlow : workFlowList) {
            workFlowService.save(factory, workFlow);
        }
//        fifteenTablesService.saveWorkFLow(factory, formVariables);
        WorkTicket workticket = new WorkTicket();
        workticket.setId(workend.getWorkTicketId());
        workticket.setStatus(8);
        workTicketService.update(factory, workticket);

        TicketYcInfoBean ycinfo = new TicketYcInfoBean();
        ycinfo.setWorkTicketId(workend.getWorkTicketId());
        ycinfo.setRemark(workend.getRemark());
        workycinfoservice.updateSelective(factory, ycinfo);
        return Response.success("工作票已归档", workend.getId());
    }

    @TaskAnnotation("workflowUpdateCancel")
    public Response workflowUpdateCancel(SessionFactory factory, RedirectQuery redirectQuery) {
        fifteenTablesService.workflowUpdateCancel(factory, redirectQuery);
        return Response.success("工作票已作废");
    }

    public void deleteById(SessionFactory factory, String id) {
        fifteenTablesService.deleteById(factory, id);
    }

    /**
     * 详情
     *
     * @param factory
     * @param id
     * @return
     */
    public YcLineFirstDto getById(SessionFactory factory, String id) {
        // 主表
        WorkTicket workTicket = workTicketService.queryById(factory, id);
        // 工作票基础信息
        TicketBaseMsg ticketBaseMsg = yccommonservice.queryTicketMsg(factory, id);
        // 高处票基础信息
        TicketYcInfoBean ycInfo = workycinfoservice.getYcInfo(factory, id);
        // 签名信息
        List<WorkFlow> workFlows = workFlowService.queryByTicketId(factory, id);
        // 工作内容
        List<YcContentBean> ycInfolist = yccontentservice.getYcInfolist(factory, id);
        // 终结信息
        List<WorkEnd> endlist = workEndService.getId(factory, id);

        YcLineFirstDto dto = new YcLineFirstDto();
        BeanUtil.copyProperties(workTicket, dto);
        BeanUtil.copyProperties(ticketBaseMsg, dto);
        BeanUtil.copyProperties(ycInfo, dto);
//        dto.setHeatanAlysislist(heatAnalysisBeans);
        dto.setId(workTicket.getId());
        dto.setWorkFlowList(workFlows);
        dto.setContentlist(ycInfolist);

        TicketChange ticketChange = ticketChangeService.getByWorkTicketId(factory, id);
        dto.setTicketChange(ticketChange);
        dto.setWorkEndList(endlist);
        return dto;
    }


    /**
     * 高处作业票、动火安全作业票
     * 获取打印数据
     *
     * @param factory
     * @param ticketId
     * @return
     */
    @TaskAnnotation("getPrintData")
    public List<YcLineFirstPrint> getPrintData(SessionFactory factory, String ticketId) {
        List<YcLineFirstPrint> printlist = new ArrayList();

        // 作业类别字典
//        Map<String, String> dataDictByCode = DataDictTool.getDataDictByCode("twoTicketManagement", "ticket_task_type");

        // 主表信息
        WorkTicket workTicket = workTicketService.queryById(factory, ticketId);
        // 水司信息
        TicketYcInfoBean ycInfo = workycinfoservice.getYcInfo(factory, ticketId);
        // 所有签名信息
        List<WorkFlow> workFlowlist = workFlowService.queryByTicketId(factory, ticketId);
        // 基础信息
        TicketBaseMsg ticketBaseMsg = yccommonservice.queryTicketMsg(factory, ticketId);
        // 工作终结信息
        List<WorkEnd> workendlist = workendservice.getList(factory, ticketId);
        // 应挂的接地线
        SecurityArrangementBean securityarrangementbean = new SecurityArrangementBean();
        fifteenTablesService.securityArrangementForPrint(factory, ticketId, securityarrangementbean);

        String workDeptStr = "";
        String memberNameStr = "";
        // 工作票状态
        Integer status = workTicket.getStatus();
        // 工作班组、作业单位
        if (CollUtil.isNotEmpty(ticketBaseMsg.getWorkTeamList())) {
            workDeptStr = ticketBaseMsg.getWorkTeamList().stream().map(WorkTeam::getWorkTeamName).collect(Collectors.joining(","));
        }
        // 工作班组成员、作业人
        if (CollUtil.isNotEmpty(ticketBaseMsg.getWorkMemberList())) {
            memberNameStr = ticketBaseMsg.getWorkMemberList().stream().map(WorkMember::getMemberName).collect(Collectors.joining(","));
        }

        YcLineFirstPrint yclinefirstprint = new YcLineFirstPrint();
        // 应挂的接地线
        BeanUtil.copyProperties(securityarrangementbean, yclinefirstprint, false);
        yclinefirstprint.setUnitName(workTicket.getUnitName());
        yclinefirstprint.setDirectorName(workTicket.getDirectorName());
        yclinefirstprint.setPowerNameDouble(ycInfo.getPowerNameDouble());
        yclinefirstprint.setCode(workTicket.getCode());
        // 安全措施
        yclinefirstprint.setSafetyMsg(ycInfo.getSafetyMsg());
        yclinefirstprint.setStopPower(ycInfo.getStopPower());
        yclinefirstprint.setEquipmentName(ycInfo.getEquipmentName());

        // 工作班组
        yclinefirstprint.setWorkTeamStr(workDeptStr);
        // 工作成员
        yclinefirstprint.setWriteWorkMemberStr(memberNameStr);


        yclinefirstprint.setEstimatedStartTime(DateUtil.format(workTicket.getEstimatedStartTime(), "yyyy年MM月dd日 HH时mm分"));
        yclinefirstprint.setEstimatedEndTime(DateUtil.format(workTicket.getEstimatedEndTime(), "yyyy年MM月dd日 HH时mm分"));

        yclinefirstprint.setRemark(ycInfo.getRemark());

        // 签发信息
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.SIGN.getStatus()) {

                Predicate<WorkFlow> predicateNode = f -> ObjectUtil.isNotNull(f.getStatus()) && f.getStatus().equals(TicketStatus.SIGN.getStatus());//过滤节点
                Predicate<WorkFlow> sig = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Predicate<WorkFlow> acc = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name1");

                // 签发人
                Optional<WorkFlow> optsig = workFlowlist.stream().filter(predicateNode.and(sig)).findFirst();
                Date sigTime = optsig.map(WorkFlow::getOperationTime).orElse(null);
                yclinefirstprint.setSignPerson(optsig.map(WorkFlow::getName).orElse(""));
                yclinefirstprint.setWorksignTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));

                // 负责人
                Optional<WorkFlow> optdirector = workFlowlist.stream().filter(predicateNode.and(acc)).findFirst();
                Date directorTime = optdirector.map(WorkFlow::getOperationTime).orElse(null);
                yclinefirstprint.setSignDirectorPerson(optdirector.map(WorkFlow::getName).orElse(""));
                yclinefirstprint.setSignDirectorTime(DateUtil.format(directorTime, "yyyy年MM月dd日 HH时mm分"));
            }
        }

        // 许可信息
        if (CollUtil.isNotEmpty(workFlowlist)) {
            if (status >= TicketStatus.PERMIT.getStatus()) {

                Predicate<WorkFlow> predicateNode = f -> ObjectUtil.isNotNull(f.getStatus()) && f.getStatus().equals(TicketStatus.PERMIT.getStatus());//过滤节点
                Predicate<WorkFlow> sig = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");

                // 许可人
                Optional<WorkFlow> optsig = workFlowlist.stream().filter(predicateNode.and(sig)).findFirst();
                Date sigTime = optsig.map(WorkFlow::getCreateTime).orElse(null);
                yclinefirstprint.setPermitPerson(optsig.map(WorkFlow::getName).orElse(""));
                yclinefirstprint.setWorkPermitTime(DateUtil.format(sigTime, "yyyy年MM月dd日 HH时mm分"));

                // 22.3 工作票变更信息
                TicketChange ticketChange = ticketChangeService.getByWorkTicketId(factory, ticketId);
                List<TicketChangeDirector> ticketChangeDirectorList = ticketChange.getTicketChangeDirectorList().stream().sorted(Comparator.comparing(TicketChangeDirector::getChangeTime).reversed()).collect(Collectors.toList());
                List<TicketChangeDelay> ticketChangeDelayList = ticketChange.getTicketChangeDelayList().stream().sorted(Comparator.comparing(TicketChangeDelay::getCreateTime).reversed()).collect(Collectors.toList());
                List<TicketChangeMember> ticketChangeMemberList = ticketChange.getTicketChangeMemberList().stream().sorted(Comparator.comparing(TicketChangeMember::getChangeTime).reversed()).collect(Collectors.toList());
                ;

                // 变更负责人
                if (CollUtil.isNotEmpty(ticketChangeDirectorList)) {
                    TicketChangeDirector ticketChangeDirector = ticketChangeDirectorList.get(0);
                    yclinefirstprint.setOldDirectorName(ticketChangeDirector.getFormerDirectorName());
                    yclinefirstprint.setNewDirectorName(ticketChangeDirector.getNewDirectorName());
                    yclinefirstprint.setSigName(ticketChangeDirector.getSingName());
                    yclinefirstprint.setChangeDirectorTime(DateUtil.format(ticketChangeDirector.getChangeTime(), "yyyy年MM月dd日 HH时mm分"));
                }

                // 变更延期
                if (CollUtil.isNotEmpty(ticketChangeDelayList)) {
                    TicketChangeDelay ticketchangedelay = ticketChangeDelayList.get(0);
                    yclinefirstprint.setChangeDelayTime(DateUtil.format(ticketchangedelay.getDelayTime(), "yyyy年MM月dd日 HH时mm分"));
                    yclinefirstprint.setYcfzr(ticketchangedelay.getDirectorName());
                    yclinefirstprint.setYcxkr(ticketchangedelay.getDutyPersonName());
                }

                // 变更成员
                if (CollUtil.isNotEmpty(ticketChangeMemberList)) {
                    TicketChangeMember ticketchangemember = ticketChangeMemberList.get(0);
                    yclinefirstprint.setChangeDelayTime(DateUtil.format(ticketchangemember.getChangeTime(), "yyyy年MM月dd日 HH时mm分"));
                    yclinefirstprint.setSubPerson(ticketchangemember.getDecreaseMemberName());
                    yclinefirstprint.setAddPerson(ticketchangemember.getAddMemberName());
                }

            }

        }


        // 工作票终结信息
        if (CollUtil.isNotEmpty(workendlist)) {
            if (status >= TicketStatus.TICKET_END.getStatus()) {
                WorkEnd workEnd = workendlist.get(0);
//                    List<String> split = StrUtil.split(endInformation, ";");
//                    yclinefirstprint.setJdNumber(split.get(0));
//                    yclinefirstprint.setJdQty(split.get(1));
                yclinefirstprint.setJdNumber(workEnd.getEndInformation());
                yclinefirstprint.setQtyzu(workEnd.getEndDescription());



                Predicate<WorkFlow> predicateNode = f -> ObjectUtil.isNotNull(f.getStatus()) && f.getStatus().equals(TicketStatus.TICKET_END.getStatus());//过滤节点
                Predicate<WorkFlow> one = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name0");
                Predicate<WorkFlow> two = f -> StrUtil.isNotBlank(f.getType()) && f.getType().equals("name1");

                // 指定专责监护人
                Optional<WorkFlow> oneOpt = workFlowlist.stream().filter(predicateNode.and(one)).findFirst();
                // 负责监护
                Optional<WorkFlow> twoOpt = workFlowlist.stream().filter(predicateNode.and(two)).findFirst();

                yclinefirstprint.setZdzzjhr(oneOpt.map(WorkFlow::getName).orElse(""));
                yclinefirstprint.setFzjh(twoOpt.map(WorkFlow::getName).orElse(""));
            }
        }

        // 工作内容
        List<YcContentBean> ycInfolist = yccontentservice.getYcInfolist(factory, ticketId);
        if (CollUtil.isNotEmpty(ycInfolist)) {
            for (YcContentBean ycContentBean : ycInfolist) {
                YcLineFirstPrint centenPrintbean = new YcLineFirstPrint();
                BeanUtil.copyProperties(yclinefirstprint, centenPrintbean);
                centenPrintbean.setContentEq(ycContentBean.getEquipmentName());
                centenPrintbean.setContentCt(ycContentBean.getContent());
                printlist.add(centenPrintbean);
            }
        }
        return printlist;
    }
}
